//package vip.xiaomaoxiaoke.functiondemo.core;
//
//import com.alibaba.fastjson.JSONObject;
//import org.springframework.beans.BeansException;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.beans.factory.config.BeanPostProcessor;
//import org.springframework.core.MethodParameter;
//import org.springframework.http.HttpEntity;
//import org.springframework.http.HttpHeaders;
//import org.springframework.http.MediaType;
//import org.springframework.stereotype.Component;
//import org.springframework.web.bind.annotation.GetMapping;
//import org.springframework.web.bind.annotation.PostMapping;
//import org.springframework.web.bind.annotation.RequestMapping;
//import org.springframework.web.bind.annotation.RequestMethod;
//import org.springframework.web.client.RestTemplate;
//import org.springframework.web.context.WebApplicationContext;
//import org.springframework.web.method.HandlerMethod;
//import org.springframework.web.servlet.mvc.condition.PathPatternsRequestCondition;
//import org.springframework.web.servlet.mvc.condition.PatternsRequestCondition;
//import org.springframework.web.servlet.mvc.condition.RequestMethodsRequestCondition;
//import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
//import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
//import org.springframework.web.util.pattern.PathPattern;
//import vip.xiaomaoxiaoke.functiondemo.annotation.ParamDescription;
//import vip.xiaomaoxiaoke.functiondemo.annotation.PlugDescription;
//import vip.xiaomaoxiaoke.functiondemo.dto.FunctionDefinition;
//import vip.xiaomaoxiaoke.functiondemo.dto.Tool;
//
//import javax.annotation.PostConstruct;
//import java.lang.reflect.Field;
//import java.lang.reflect.Method;
//import java.lang.reflect.Parameter;
//import java.util.*;
//
//@Component
//public class MyBeanPostProcessor {
//
//
//    @Autowired
//    private WebApplicationContext applicationContext;
//
//
//    @PostConstruct
//    public void getAllURL() {
//        // 获取springmvc处理器映射器组件对象 RequestMappingHandlerMapping无法直接注入
//        Map<RequestMappingInfo, HandlerMethod> mapping = applicationContext.getBean("requestMappingHandlerMapping", RequestMappingHandlerMapping.class).getHandlerMethods();
//        //获取url与类和方法的对应信息
//
//        List<FunctionDefinition> result = new ArrayList<FunctionDefinition>();
//        for (Map.Entry<RequestMappingInfo, HandlerMethod> handlerMethodEntry : mapping.entrySet()) {
//            Map<String, Object> urlInfoMap = new HashMap<String, Object>();
//            RequestMappingInfo info = handlerMethodEntry.getKey();
//            HandlerMethod method = handlerMethodEntry.getValue();
//            // 获取url
//            PathPatternsRequestCondition patternsCondition = info.getPathPatternsCondition();
//
//            // 反射获取url对应类名和方法名
//            // 类名
//            PlugDescription plugDescription = method.getMethod().getAnnotation(PlugDescription.class);
//
//            if (plugDescription != null) {
//
//                FunctionDefinition functionDefinition = new FunctionDefinition();
//                functionDefinition.setPlugDescription(plugDescription.value());
//                Set<PathPattern> patterns = patternsCondition.getPatterns();
//                for (PathPattern url : patterns) {
//                    functionDefinition.setUrl(url.getPatternString());
//                }
//                MethodParameter[] methodParameters = method.getMethodParameters();
//                Class<?> parameterType = methodParameters[0].getParameterType();
//
//
//                Field[] declaredFields = parameterType.getDeclaredFields();
//
//                Map<String, Tool.ParamDescription> properties = new HashMap<>();
//                Arrays.stream(declaredFields).forEach((field -> {
//                    ParamDescription annotation = field.getAnnotation(ParamDescription.class);
//                    properties.put(field.getName(), new Tool.ParamDescription(field.getType().getName().toString(), annotation.value()));
//
//                }));
//
//                functionDefinition.setTool(Tool.of(method.getMethod().getName(), plugDescription.value(), properties));
//
//
//                urlInfoMap.put("className", method.getMethod().getDeclaringClass().getName());
//                functionDefinition.setMethodName(method.getMethod().getName());
//
//                // 获取请求类型
//                RequestMethodsRequestCondition methodsRequestCondition = info.getMethodsCondition();
//                List<String> requestMethodList = new ArrayList<String>(methodsRequestCondition.getMethods().size());
//                for (RequestMethod requestMethod : methodsRequestCondition.getMethods()) {
//                    requestMethodList.add(requestMethod.toString());
//
//                }
//                functionDefinition.setRequestMethodList(requestMethodList);
//                urlInfoMap.put("type", requestMethodList);
//
//                result.add(functionDefinition);
//            }
//
//        }
//
//
//
//        HttpHeaders headers = new HttpHeaders();
//        headers.setContentType(MediaType.APPLICATION_JSON);
//
//        // 创建请求体的数据
//        String requestBody = JSONObject.toJSONString(result);
//
//        // 创建HttpEntity对象，包含请求头和请求体
//        HttpEntity<String> entity = new HttpEntity<>(requestBody, headers);
//
//        // 使用RestTemplate发送POST请求
//        RestTemplate restTemplate = new RestTemplate();
//        String response = restTemplate.postForObject("http://127.0.0.1:9000/api/register/function", entity, String.class);
//
//        System.out.println(response);
//    }
//
//
//}
